//
//  NSObject+DGGBridge.m
//  DGGMiniProgramSDK
//
//  Created by 胡中楷 on 2020/3/28.
//

#import "NSObject+DGGBridge.h"
#import "WKWebViewJavascriptBridge.h"
#include <objc/runtime.h>

static NSString * const kBridgeObjectKey = @"bridgeObjectKey";
static NSString * const kWebObjectKey = @"webObjectKey";

@implementation NSObject (DGGBridge)

- (void)injectBridgeWithWeb:(WKWebView *)webView{
    self.bridgeWeb = webView;
#if DEBUG
    [WKWebViewJavascriptBridge enableLogging];
#endif
    self.bridge = [WKWebViewJavascriptBridge bridgeForWebView:webView];
}

- (void)setBridge:(WKWebViewJavascriptBridge *)bridge{
    objc_setAssociatedObject(self, &kBridgeObjectKey, bridge, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (WKWebViewJavascriptBridge *)bridge{
    return objc_getAssociatedObject(self, &kBridgeObjectKey);
}

- (void)setBridgeWeb:(WKWebView *)bridgeWeb{
    objc_setAssociatedObject(self, &kWebObjectKey, bridgeWeb, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (WKWebView *)bridgeWeb{
    return objc_getAssociatedObject(self, &kWebObjectKey);
}

@end
